package com.gpframework.module.ctn.service.impl;

import cn.hutool.core.convert.Convert;
import cn.hutool.core.lang.TypeReference;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.gpframework.common.utils.StringUtils;
import com.gpframework.module.ctn.domain.*;
import com.gpframework.module.ctn.domain.req.GpHoldGoodsChinaReleaseAdd;
import com.gpframework.module.ctn.domain.vo.GpHoldGoodsChinaReleaseExcel;
import com.gpframework.module.ctn.domain.vo.GpHoldGoodsChinaReleaseVo;
import com.gpframework.module.ctn.mapper.GpHoldGoodsChinaReleaseMapper;
import com.gpframework.module.ctn.service.IGpHoldGoodsChinaReleaseService;
import org.apache.commons.collections4.CollectionUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;
/**
 * @ClassName GpHoldGoodsChinaRelease
 * @Author gpframework
 * @Date 2024-01-08
 * @Version 1.0.0
 * @Description 扣货国内放货Service业务层处理
 */
@Service
public class GpHoldGoodsChinaReleaseServiceImpl extends ServiceImpl<GpHoldGoodsChinaReleaseMapper, GpHoldGoodsChinaRelease> implements IGpHoldGoodsChinaReleaseService {

    @Autowired
    private GpReceiptGoodsServiceImpl gpReceiptGoodsService;

    @Autowired
    private GpCtnOrderServiceImpl gpCtnOrderService;
    @Autowired
    private GpPackagesInformationServiceImpl gpPackagesInformationService;
    @Autowired
    private GpSackInformationServiceImpl gpSackInformationService;

    /**
     * @Description 查询列表返回VO 用于返回给前端的列表接口
     * @Author gpframework
     * @Date 2024-01-08
     * @Param [lqw]
     * @Return List<GpHoldGoodsChinaReleaseVo>
     */
    @Override
    public List<GpHoldGoodsChinaReleaseVo> listVo(QueryWrapper<GpHoldGoodsChinaRelease> lqw) {
        return this.baseMapper.listVo(lqw);
    }

    /**
     * @Description 通过查询详情VO 用于返回给前端的列详情接口
     * @Author gpframework
     * @Date 2024-01-08
     * @Param [id]
     * @Return GpHoldGoodsChinaReleaseVo
     */
    @Override
    public GpHoldGoodsChinaReleaseVo getVoById(Long id) {
        return this.baseMapper.getVo(id);
    }

    @Override
    public List<GpHoldGoodsChinaReleaseExcel> gpHoldGoodsChinaReleaseImport(List<GpHoldGoodsChinaReleaseExcel> excelList, Long brandId, Long storeId) {
        //导入失败的列表
        List<GpHoldGoodsChinaReleaseExcel> failureList = new ArrayList<>();
        //扣货国内放货
        GpHoldGoodsChinaRelease gpHoldGoodsChinaRelease;

        for (int i = 0; i < excelList.size(); i++) {
            GpHoldGoodsChinaReleaseExcel excel = excelList.get(i);
            if (StringUtils.isNull(excel)) {
                excel = new GpHoldGoodsChinaReleaseExcel();
                excel.setSubscript(i+1);
                excel.setMsg("读取数据失败");
                failureList.add(excel);
                continue;
            }
            gpHoldGoodsChinaRelease = Convert.convert(new TypeReference<GpHoldGoodsChinaRelease>() {}, excel);
            this.save(gpHoldGoodsChinaRelease);
        }
        return failureList;
    }

    @Override
    public boolean addBatch(GpHoldGoodsChinaReleaseAdd gpHoldGoodsChinaReleaseAdd) {
        List<GpHoldGoodsChinaRelease> gpHoldGoodsChinaReleaseList = gpHoldGoodsChinaReleaseAdd.getGpHoldGoodsChinaReleaseList();
        if (CollectionUtils.isNotEmpty(gpHoldGoodsChinaReleaseList)){
            for (int i = 0; i < gpHoldGoodsChinaReleaseList.size(); i++) {
                GpHoldGoodsChinaRelease gpHoldGoodsChinaRelease = gpHoldGoodsChinaReleaseList.get(i);
                if (CollectionUtils.isNotEmpty(gpHoldGoodsChinaRelease.getPackagesInformationList())){
                    //扣货国内放货打包信息列表
                    this.packagesHoldGoodsChinaRelease(gpHoldGoodsChinaRelease);
                }
                if (CollectionUtils.isNotEmpty(gpHoldGoodsChinaRelease.getSackInformationList())){
                    //扣货国内放货散货信息列表
                    this.sackInHoldGoodsChinaRelease(gpHoldGoodsChinaRelease);
                }
            }
        }

        return true;
    }

    private void sackInHoldGoodsChinaRelease(GpHoldGoodsChinaRelease gpHoldGoodsChinaRelease) {
        for (GpSackInformation gpSackInformation : gpHoldGoodsChinaRelease.getSackInformationList()) {
            GpHoldGoodsChinaRelease goodsChinaRelease = Convert.convert(new TypeReference<GpHoldGoodsChinaRelease>() {}, gpSackInformation);
            goodsChinaRelease.setReleaseCustomer(gpSackInformation.getReleaseCustomer());
            goodsChinaRelease.setReleaseCustomerTel(gpSackInformation.getReleaseCustomerTel());
            goodsChinaRelease.setChinaReleaseDate(new Date());
            // 判断新增还是修改
            if (gpSackInformation.getHoldChinaReleaseId()!=null){
                goodsChinaRelease.setId(gpSackInformation.getHoldChinaReleaseId());
            }else {
                goodsChinaRelease.setId(null);
            }
            this.saveOrUpdate(goodsChinaRelease);

            //更新打包信息发送短信
            GpSackInformation sackInformation = gpSackInformationService.getById(gpSackInformation.getId());
            sackInformation.setReleaseCustomer(gpSackInformation.getReleaseCustomer());
            sackInformation.setReleaseCustomerTel(gpSackInformation.getReleaseCustomerTel());
            sackInformation.setHoldChinaReleaseId(goodsChinaRelease.getId());
            gpSackInformationService.updateById(sackInformation);

        }
    }

    private void packagesHoldGoodsChinaRelease(GpHoldGoodsChinaRelease gpHoldGoodsChinaRelease) {
        for (GpPackagesInformation gpPackagesInformation : gpHoldGoodsChinaRelease.getPackagesInformationList()) {
            GpHoldGoodsChinaRelease goodsChinaRelease = Convert.convert(new TypeReference<GpHoldGoodsChinaRelease>() {}, gpPackagesInformation);
            goodsChinaRelease.setReleaseCustomer(gpPackagesInformation.getReleaseCustomer());
            goodsChinaRelease.setReleaseCustomerTel(gpPackagesInformation.getReleaseCustomerTel());
            goodsChinaRelease.setChinaReleaseDate(new Date());
            // 判断新增还是修改
            if (gpPackagesInformation.getHoldChinaReleaseId()!=null){
                goodsChinaRelease.setId(gpPackagesInformation.getHoldChinaReleaseId());
            }else {
                goodsChinaRelease.setId(null);
            }
            this.saveOrUpdate(goodsChinaRelease);
            //更新打包信息发送短信
            GpPackagesInformation packagesInformation = gpPackagesInformationService.getById(gpPackagesInformation.getId());
            packagesInformation.setReleaseCustomer(gpPackagesInformation.getReleaseCustomer());
            packagesInformation.setReleaseCustomerTel(gpPackagesInformation.getReleaseCustomerTel());
            packagesInformation.setHoldChinaReleaseId(goodsChinaRelease.getId());
            gpPackagesInformationService.updateById(packagesInformation);
        }

    }

    @Override
    public boolean addHoldGoodsChinaRelease(GpHoldGoodsChinaReleaseAdd gpHoldGoodsChinaReleaseAdd) {
        GpHoldGoodsChinaRelease gpHoldGoodsChinaRelease = Convert.convert(new TypeReference<GpHoldGoodsChinaRelease>() {
        }, gpHoldGoodsChinaReleaseAdd);
        GpCtnOrder gpCtnOrder = gpCtnOrderService.getById(gpHoldGoodsChinaRelease.getCtnOrderId());
        GpReceiptGoods gpReceiptGoods = gpReceiptGoodsService.getById(gpHoldGoodsChinaRelease.getReceiptId());
        GpPackagesInformation gpPackagesInformation = gpPackagesInformationService.getById(gpHoldGoodsChinaRelease.getPackId());
        if (gpCtnOrder!=null){
            //从定货柜拿
            gpHoldGoodsChinaRelease.setAgentCtnNo(gpCtnOrder.getAgentCtnNo());
        }
        if (gpReceiptGoods!=null){
            //从订单管理拿
            gpHoldGoodsChinaRelease.setCustomer(gpReceiptGoods.getCustomer());
            gpHoldGoodsChinaRelease.setCustomerTel(gpReceiptGoods.getCustomerTel());
            gpHoldGoodsChinaRelease.setSupplier(gpReceiptGoods.getSupplier());
            gpHoldGoodsChinaRelease.setSupplierTel(gpReceiptGoods.getSupplierTel());
            gpHoldGoodsChinaRelease.setReceiptNo(gpReceiptGoods.getReceiptNo());
            gpHoldGoodsChinaRelease.setGoodsDescription(gpReceiptGoods.getGoodsDescription());
        }
        if (gpPackagesInformation!=null){
            //从打包信息里面拿
            gpHoldGoodsChinaRelease.setPackNo(gpPackagesInformation.getPackNo());
            gpHoldGoodsChinaRelease.setWeight(gpPackagesInformation.getPackWeight());
            gpHoldGoodsChinaRelease.setUnit(gpPackagesInformation.getUnit());
            gpHoldGoodsChinaRelease.setCbm(gpPackagesInformation.getCbm());
            gpHoldGoodsChinaRelease.setHoldOrNot(gpPackagesInformation.getHoldOrNot());
        }


        return this.save(gpHoldGoodsChinaRelease);
    }




}
